<template>
  <view class="page">
    <Tab
      ref="Tab"
      :tabList="tabList"
      :tabStyle="true"
      :selectIndex="selectIndex"
      @tabToggle="changeTab"
    />
    <view class="content">
      <view class="box" v-for="(item, i) in clocks" :key="i">
        <view class="left">
          <view class="main_time">
            {{ item.setTime }}
            <span class="mark"> | {{ item.mark }}</span></view
          >
          <view class="tips">
            {{ item.type | filterType }} |
            {{ item.setTime | relativeTime }}</view
          >
        </view>
        <view class="right">
          <switch :checked="!!item.isOpen" />
        </view>
      </view>
    </view>
    <view class="add" @tap="toAddClock">+</view>
  </view>
</template>

<script>
import Tab from "../../components/Tab";
import { getFile, setAlarm, setAlarm1 } from "../../utils/utils";

export default {
  components: { Tab },
  data() {
    return {
      title: "Hello",
      select: true,
      tabList: [{ name: "闹钟" }, { name: "时钟" }, { name: "计时" }],
      selectIndex: 0,
      clocks: [{ setTime: "12.30", type: 1, isOpen: 1, mark: "test" }],
    };
  },
  onLoad(option) {
    console.log(option, "option");
  },
  onShow() {
    uni.$on("myNewAddressData", (res) => {
      console.log("获取返回的地址", JSON.parse(res.myNewAddressData));
      const params = JSON.parse(res.myNewAddressData);
      return;
      this.clocks.push({
        ...JSON.parse(res.myNewAddressData),
        setTime: Date.now(),
        isOpen: 1,
      });
      // this.setColok(18, 54, '闹钟')
      // 清除监听
      uni.$off("myNewAddressData");
    });
  },
  methods: {
    changeTab({ data, index }) {
      console.log("test");
      this.selectIndex = index;
      this.$refs.Tab.setIndex(index);
    },
    setColok(hour, minutes, message) {
      console.log("setColok: ");
      setAlarm(hour, minutes, message);
    },
    getClocks() {
      // getFile()
    },
    toAddClock() {
      uni.navigateTo({
        url: "/pages/addClock/index?name=1",
      });
    },
  },
};
</script>

<style lang="scss">
.page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .content {
    position: relative;
    margin-top: 10px;
    width: 100%;
    .box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin: 10px 20px;
      .left {
        .main_time {
          font-size: 20px;
          color: #333;
          .mark {
            font-size: 14px;
            color: #999;
          }
        }
        .tips {
          font-size: 14px;
          color: #999;
        }
      }
    }
  }
  .add {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    box-shadow: -2px -2px 3px skyblue;
    position: absolute;
    right: 50px;
    bottom: 50px;
    text-align: center;
    line-height: 40px;
    font-size: 50px;
    color: skyblue;
    background-color: #eee;
    cursor: pointer;
  }
}
</style>
